﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Drawing.Imaging;
using System.Reflection;

namespace SuchSofts.OpenImage.Lib.Operation
{
    internal class CommonOperations
    {
        public static void Navigate(string page)
        {
            try
            {
                Process.Start(page);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occured while loading " + page + "\nDetails: " + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public static void Browse(string folder)
        {
            try
            {
                Process.Start("explorer", folder);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occured while loading " + folder + "\nDetails: " + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public static string ConvertToLegalFileName(string name)
        {
            //replace \ / : * ? " < > | with #
            Regex regexCharFilter = new Regex("[\\u005C|/|:|\\u002A|\\u003F|\"|<|>| \\u007C]");
            string fileName = regexCharFilter.Replace(name, "#");
            string old = fileName;
            if (fileName == "")
            {
                fileName = "#";
            }
            return fileName;
        }

        public static void EmptyFolder(string path)
        {
            DirectoryInfo folder = new DirectoryInfo(path);
            foreach (DirectoryInfo subFolder in folder.GetDirectories())
            {
                EmptyFolder(subFolder.FullName);
                subFolder.Delete();
            }
            foreach (FileInfo file in folder.GetFiles())
            {
                file.Delete();
            }
        }

        public static bool IsFileExisted(string path, ref string name)
        {
            if (File.Exists(path + name))
            {
                string mainName = name;
                string exName = "";
                int exStartIndex = name.LastIndexOf(".");
                if (exStartIndex != -1)
                {
                    mainName = name.Substring(0, exStartIndex);
                    exName = name.Substring(exStartIndex);
                }

                for (long i = 1; File.Exists(name = (mainName + i.ToString() + exName)); i++) ;

                return false;
            }
            else
            {
                return true;
            }
        }

        public static string GetImageExtension(System.Drawing.Image image)
        {
            Type Type = typeof(ImageFormat);
            System.Reflection.PropertyInfo[] imageFormatList = Type.GetProperties(BindingFlags.Static | BindingFlags.Public);
            for (int i = 0; i != imageFormatList.Length; i++)
            {
                ImageFormat formatClass = (ImageFormat)imageFormatList[i].GetValue(null, null);
                if (formatClass.Guid.Equals(image.RawFormat.Guid))
                {
                    return imageFormatList[i].Name.ToLower();
                }
            }
            return "";
        }
    }
}
